﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace BooksManagermentSystem
{
    public partial class AdminPI_form : Form
    {
        public AdminPI_form()
        {
            InitializeComponent();
        }
        bool flag;
        private void AdminPI_form_Load(object sender, EventArgs e)
        {
            panel1.Visible= false;
            flag = false;
            string account = this.Tag.ToString();
            textBox1.Text = account;
            textBox2.Text = DBhelper.GetDataRow($"select name from AdminInfo where account='{account}'")["name"].ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if(flag)
            {
                string oldpwd = textBox4.Text;
                string newpwd = textBox3.Text;
                if (oldpwd.Length == 0)
                {
                    MessageBox.Show("请输入原密码");
                    return;
                }
                if(newpwd.Length == 0)
                {
                    MessageBox.Show("请输入新密码");
                    return;
                }
                if (!DBhelper.checkAdmin(Tag.ToString(), oldpwd))
                {
                    MessageBox.Show("原密码不正确");
                    return;
                }
                string sql = $"update AdminInfo set pwd = '{newpwd}' where pwd = '{oldpwd}' and account = '{Tag.ToString()}'";
                if(DBhelper.NonQuery(sql))
                {
                    MessageBox.Show("修改成功！");
                    textBox3.Clear();
                    textBox4.Clear();
                    panel1.Visible= false;
                    flag = false;
                    return;
                }
                else
                {
                    MessageBox.Show("修改失败！");
                    return;
                }
            }
            panel1.Visible = true;
            flag = true;
        }
    }
}
